package com.saascloud.redis.config;

import com.saascloud.redis.core.SaasStringRedisTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;

/**
 * @Author: liangxu
 * @Contact: one_dream@aliyun.com
 * @Date: 2022/5/13 16:38
 * @Version: 1.0
 * @Description:
 */
@Slf4j
public class SaasRedisAutoConfiguration {

//    @Bean
//    public RedisTemplate<String, Object> redisObjectTemplate(RedisConnectionFactory redisConnectionFactory) {
//        SaasRedisKeySerializer saasRedisKeySerializer = new SaasRedisKeySerializer();
//        RedisTemplate<String, Object> template = new RedisTemplate<>();
//        // 设置 RedisConnection 工厂。😈 它就是实现多种 Java Redis 客户端接入的秘密工厂。感兴趣的胖友，可以自己去撸下。
//        template.setConnectionFactory(redisConnectionFactory);
//        // 使用 String 序列化方式，序列化 KEY 。
//        template.setKeySerializer(saasRedisKeySerializer);
//        template.setHashKeySerializer(RedisSerializer.string());
//        // 使用 JSON 序列化方式（库是 Jackson ），序列化 VALUE 。
//        template.setValueSerializer(RedisSerializer.json());
//        template.setHashValueSerializer(RedisSerializer.json());
//        return template;
//    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public SaasStringRedisTemplate saasStringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        return new SaasStringRedisTemplate(redisConnectionFactory);
    }
}
